package 面试;

public class 求两个字符串的最长公共子串 {
    /**
     * 二维矩阵法
     * @author https://blog.csdn.net/u010397369/article/details/38979077
     */

    public static int getLCSLength(String s1, String s2) {
        int result = 0;
        int[][] array = new int[s1.length()][s2.length()];
        for (int i = 0; i < s1.length(); i++) {
            for (int j = 0; j < s2.length(); j++) {
                if (s1.charAt(i) == s2.charAt(j)) {
                    if (j > 0 && i > 0) {
                        array[i][j] = 1 + array[i - 1][j - 1];
                    } else {
                        array[i][j] = 1;
                    }
                    result = Math.max(result, array[i][j]);
                } else {
                    array[i][j] = 0;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getLCSLength("acbcbcef", "abcbced"));
    }
}
